$(function() {
    // 添加参数
    var param_cnt = 1;
    $("#add_parameter_btn").click(function () {
        param_cnt++;
        add_parameter(param_cnt, "", "","params_p","p",$(this));
    })
    // 添加header
    var header_cnt = 1;
    $("#add_api_headers").click(function () {
        header_cnt++;
        add_parameter(header_cnt, "", "","headers_p","h",$(this));
    })
    //发送请求

    $("#http_send").click(function() {

    var method = $("#api_method").val();
    var http_url = $("#http_url_input").val();
    var params = get_all_type("params_p","p");
    var headers = get_all_type("headers_p","h");
  if (is_empty(http_url)) {
    alert("接口链接为空，请填写后再请求！");
    return;
  }
  if (!params) {
    alert("参数填写为空，请填写完整之后再试！");
    return;
  }
 var that =$(this)
      $.ajax({
        type: method, 
        url:  http_url, 
        dataType: "json", //预期的服务器响应的数据类型 
        data: params_2_type(params,method), 
        headers:params_2_json(headers),
        beforeSend: function() {
            that.text("正在请求");
        },
        success: function(res) {
            var data = res.data
            that.text("发送请求");
            $("#jsoneditor").val(JSON.stringify(data));
        },
        error: function(e) {
            that.text("发送请求");
            console.log(e)
        }
      });
    });



  });

  function add_parameter(cnt, name, value,title,_type,that) {
      //
    if (name == "undefined" || name == null) {
      name = "";
    }
    if (value == "undefined" || value == null) {
      value = "";
    }
    that.parents("#foot_end").before(
      '<tr class='+title+' cnt=' +
        cnt +
        '><td><input style="width:80%" class="form-control inputVal" type="text" name="'+_type+'_name_' +
        cnt +
        '" placeholder="Key" value="' +
        name +
        '" maxlength="100"/></td><td><input style="width:70%" class="form-control inputVal left" type="text" name="'+_type+'_value_' +
        cnt +
        '" placeholder="Value"" value="' +
        value +
        '" maxlength="5000"/><button class="btn btn-danger left" onclick="javascript:del_param(this);" type="button">删除参数</button>							</td>						</tr>					'
    );
  }

  function del_param(obj) {
    $(obj)
      .parent()
      .parent()
      .remove();
  }
  function get_all_type(title,type) {
      // params_p  p
      //headers_p  h
    var _array = [];
    $('.'+ title).each(function() {
      var cnt = $(this).attr("cnt");
      var name = $(this)
        .find("input[name="+ type +"_name_" + cnt + "]")
        .val();
      var value = $(this)
        .find("input[name="+ type +"_value_" + cnt + "]")
        .val();
      if (is_empty(name) || is_empty(value)) {
        return false;
      }
      _array.push({
        name: name,
        value: value
      });
    });
    return _array;
  }
  function is_empty(str) {
    if (str == null || str == "" || str == "undefined") {
      return true;
    }
    return false;
  }
  function params_2_str(params) {
    var str = "";
    for (var i = 0; i < params.length; i++) {
      var p = params[i];
      if (p.name) {
        str = str + "&" + p.name + "=" + p.value;
      }
    }
    if (str.length > 1) {
      str = str.substring(1);
    }
    
    return str;
  }
  function params_2_json(params) {
    var obj = {};
    for (var i = 0; i < params.length; i++) {
      var p = params[i];//u2to:111
      if (p.name) {
          obj[p.name] = p.value;
      }
    }
    return obj;
  }
  function params_2_type(params,type) {
      if(type ==='get'){
        params_2_str(params)
      }else{
        params_2_json(params)
      }
  }